import request from './request';

// 获取书签
export function getBookmarksByCode({ code }) {
  return request(window.appcfg._servicesUrl + '/api/bookmark3d/code/' + code, {
    method: 'GET',
  }).then(data => {
    if (!data.data) {
      return [];
    }
    return data.data.map(e => {
      e.id = e.objectId;
      return e;
    });
  });
}

export async function addBookmark({ camera, code, name }) {
  return request(window.appcfg._servicesUrl + '/api/bookmark3d/', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ camera, code, name }),
  });
}

export async function updateBookmark({ camera, code, name, objectId }) {
  return request(window.appcfg._servicesUrl + '/api/bookmark3d/', {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ camera, code, name, objectId }),
  });
}
export async function deleteBookmark({ objectId }) {
  return request(window.appcfg._servicesUrl + '/api/bookmark3d/' + objectId, {
    method: 'DELETE',
  });
}

// 标注
export function getAllPlotLyrByProjectId(projectId) {
  return request(window.appcfg._servicesUrl + '/api/plot-layers?projectId=' + projectId, {
    method: 'GET',
  }).then(data => {
    if (!data.data) {
      return [];
    }
    return data.data;
  });
}

export function getPlotLyrByProjIdAndLyrId(projectId, layerId) {
  return request(window.appcfg._servicesUrl + `/api/plot-layers/${projectId}/${layerId}`, {
    method: 'GET',
  }).then(data => {
    return data.data;
  });
}

export function addPlotLyr({ projectId, layerName, description = '', graphics, layerId }) {
  return request(window.appcfg._servicesUrl + '/api/plot-layers', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ projectId, layerName, description, graphics, layerId }),
  }).then(data => {
    if (!data.data) {
      return [];
    }
    return data.data;
  });
}

export function updatePlotLyr({ layerName, description = '', graphics, id }) {
  return request(window.appcfg._servicesUrl + '/api/plot-layers', {
    method: 'PUT',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ layerName, description, graphics, id }),
  }).then(data => {
    if (!data.data) {
      return [];
    }
    return data.data;
  });
}
